using UnityEngine;
using System.Collections;


public class flap : MonoBehaviour
{

    public AnimationState flapanim;
    private Perlin perlin = new Perlin();
    public float weight = 0.0f;
    public float frequency = 10.0f;
    private float lastFlapTime = -1;


    // Use this for initialization
    void Start()
    {
        flapanim = animation["test3"];
        flapanim.enabled = true;
        flapanim.layer = 1;
        //flapanim.blendMode = AnimationBlendMode.Additive;
        flapanim.normalizedSpeed = 0.0f;
        flapanim.weight = 0.5f;
        flapanim.normalizedTime = 0.0f;
    }

    public void Flap(float t)
    {
        lastFlapTime = Time.time + t;
    }

    // Update is called once per frame
    void Update()
    {
        float noise = (1.0f + perlin.Noise(Time.time / 3.0f, 0.1f, 0.1f)) * 0.5f;
        float flap = (1.0f + Mathf.Sin(Time.time * frequency)) * 0.5f;
        float target;
        
        if(Time.time < lastFlapTime)
        {
            target = 1.0f;
        }
        else
        {
            target = 0.0f;
        }
        
        if(weight < target)
        {
            weight += (target - weight) * 0.5f;
        }
        else
        {
            weight += (target - weight) * 0.1f;
        }



        float res = weight * flap + (1.0f - weight) * noise;
        
       

        /*if (Input.GetKeyDown(KeyCode.Space))
        {
            Flap(0.5f);

        }*/
        flapanim.normalizedTime = res;
    }
}
